$(function () {
    layui.use(['form', 'layer','jquery','laydate','upload'],
        function() {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;


            //得到id后，根据id查询数据库，赋值给前端表单
            let newsId = sessionStorage.getItem("newsId")
            $.ajax({
                url:'/back/FindNewsTypeById',
                data:{id:newsId},
                type: 'get',
                dataType: 'json',
                success: function (res) {
                    //赋值
                    console.log(res);
                    setData(res.data);
                }
            })
            function setData(data) {
                form.val('form', {
                     "newType": data.newType // "name": "value"
                    , "remark": data.remark
                });
                $("input[type=radio][name=enable][value=" + data.enable + "]").attr("checked", "checked");
                form.render();//重新渲染表单
            }
            //监听提交
            form.on('submit(update)',
                function(data) {
                    data = data.field;
                    // console.log(data);
                    //根据id修改
                    data.id = sessionStorage.getItem("newsId")//修改servlet里没有传id，需获取从user-list.js传过来的id
                    $.ajax({
                        url:'/back/UpdateNewsType',
                        data:data,
                        type:'post',
                        dataType:'json',
                        success: function (res) {
                            if (res.count>0){
                                //发异步，把数据提交给java
                                layer.alert(res.msg, {
                                        icon: 6
                                    },
                                    function() {
                                        //关闭当前frame
                                        xadmin.close();

                                        // 可以对父窗口进行刷新
                                        xadmin.father_reload();
                                    });
                            }else {
                                layer.alert(res.msg)
                            }
                        }
                    })
                    return false;
                });

        });
})